package com.sina.shop.controller.card;

import com.sina.shop.core.exception.server.UpdateException;
import com.sina.shop.domain.card.Card;
import com.sina.shop.service.card.CardService;
import com.sina.shop.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2018-04-12-17:24
 */
@Controller
@RequestMapping("/shop-web/card")
public class CardController extends BaseController {

    @Autowired
    private CardService cardService;

    @RequestMapping(value = "/updateCard", method = RequestMethod.POST)
    public ModelAndView updateCard(Card card) {
        LOG.info("card:{}", card);

        ModelAndView mv = new ModelAndView("commonView");
        Map<String, Object> result = new HashMap<>(4);
        try {
            cardService.updateCard(card);
            result.put(CODE, OK);
            result.put(MSG, "更新购物记录成功");
        } catch (UpdateException e) {
            LOG.info("更新购物记录出现异常:{}", e.getMessage());
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);
        return mv;
    }

    @RequestMapping(value = "/getCardListByAdminId", method = RequestMethod.GET)
    public ModelAndView getCardListByAdminId(@RequestParam("adminId") Long adminId) {
        LOG.info("adminId:{}", adminId);
        ModelAndView mv = new ModelAndView("commonView");
        Map<String, Object> data = new HashMap<>(4);
        Map<String, Object> result = new HashMap<>(6);
        List<Card> cardList = cardService.getCardListByAdminId(adminId);

        data.put("cardList", cardList);
        result.put(CODE, OK);
        result.put(DATA, data);
        mv.addObject(RESULT, result);

        return mv;
    }

    @RequestMapping(value = "/getCard", method = RequestMethod.GET)
    public ModelAndView getCard(@RequestParam("id") Long id) {
        LOG.info("id:{}", id);
        ModelAndView mv = new ModelAndView("commonView");
        Map<String, Object> data = new HashMap<>(4);
        Map<String, Object> result = new HashMap<>(6);
        Card card = cardService.getCard(id);

        data.put("card", card);
        result.put(CODE, OK);
        result.put(DATA, data);
        mv.addObject(RESULT, result);

        return mv;
    }

    @RequestMapping(value = "/saveCard", method = RequestMethod.POST)
    public ModelAndView saveCard(Card card) {
        LOG.info("card:{}", card);
        card.setCreateTime(LocalDateTime.now());

        ModelAndView mv = new ModelAndView("commonView");
        Map<String, Object> result = new HashMap<>(4);
        try {
            cardService.saveCard(card);
            result.put(CODE, OK);
            result.put(MSG, "新增购物记录成功");
        } catch (UpdateException e) {
            LOG.info("新增购物记录出现异常:{}", e.getMessage());
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);
        return mv;
    }

    @RequestMapping(value = "/deleteCard", method = RequestMethod.POST)
    public ModelAndView deleteCard(@RequestParam("id") Long id) {
        LOG.info("id:{}", id);
        Map<String, Object> result = new HashMap<>(6);
        ModelAndView mv = new ModelAndView("commonView");

        try {
            cardService.deleteCard(id);
            result.put(CODE, OK);
            result.put(MSG, "删除购物记录成功");
        } catch (UpdateException e) {
            LOG.info("删除购物记录出现异常:{}", e.getMessage());
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);
        return mv;
    }
}
